package com.luo.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

/**
 * @author Zoro
 * @date 2021年08月26
 */
public class QiniuUtils {

    /**
     * 声明上传凭证
     */
    private static String accessKey = "6MtXrvOZ-gu74qydOunRi831Rkr2zzU1RbvJ5UdM";
    private static String secretKey = "28ZJX6TO_6mQakToFv8Vqf0042ZJsgek6YpvdpNB";
    private static String bucket = "ljm01";

    /**
     * 上传文件
     *
     * @param filePath 上传文件的路径
     * @param fileName 上传文件的名字
     */
    public static void upload2Qiniu(String filePath, String fileName) {
        //构造一个带指定Zone对象的配置类
        Configuration config = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(config);
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        try {
            Response response = uploadManager.put(filePath, fileName, upToken);
            //解析上传成功结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        } catch (QiniuException e) {
            Response r = e.response;
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex) {
                ex.printStackTrace();
            }

        }
    }

    /**
     * 上传文件
     *
     * @param bytes    文件的字节码
     * @param fileName 文件名
     */
    public static void upload2Qiniu(byte[] bytes, String fileName) {
        //构造一个带指定Zone对象的配置类
        Configuration config = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(config);
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(bytes, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 删除文件
     */
    public static void deleteFileQiniu(String fileName) {
        //构造一个带指定Zone对象的配置类
        Configuration config = new Configuration(Zone.zone2());
        String key = fileName;
        Auth auth = Auth.create(accessKey, secretKey);
        BucketManager bucketManager = new BucketManager(auth, config);
        try {
            bucketManager.delete(bucket, key);
        } catch (QiniuException e) {
            //如有异常，删除失败
            System.err.println(e.code());
            System.out.println(e.response.toString());

        }
    }
}
